package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.buildtable.pojo.Columns;
import com.example.buildtable.pojo.Role;
import com.example.buildtable.pojo.User;
import com.example.buildtable.pojo.UserRole;
import com.example.buildtable.service.ColumnService;
import com.example.common.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Tag(name = "栏目")
@RequestMapping(value = "/column/")
public class ColumnController {
    @Autowired
    private ColumnService columnService;

    @Operation(summary = "查询所有栏目数据")
    @GetMapping(value = "/getAllColumn")
    public R getAllColumn() {
        LambdaJoinQueryWrapper<Columns> wrapper = new LambdaJoinQueryWrapper<>(Columns.class);
        wrapper.innerJoin(Columns.class, Columns::getParentColumnId, Columns::getColumnId);
        List<Columns> columnList = columnService.list(wrapper);
        return R.ok("查询所有栏目数据成功").data("columnList", columnList);
    }

    @Operation(summary = "查询所有栏目分页数据")
    @GetMapping(value = "/getColumnPage/{page}/{limit}")
    public R getColumnPage(@PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        Page<Columns> columnsPage = new Page<>(page, limit);
        LambdaJoinQueryWrapper<Columns> wrapper = new LambdaJoinQueryWrapper<>(Columns.class);
        wrapper.innerJoin(Columns.class, Columns::getParentColumnId, Columns::getColumnId);
        columnService.page(columnsPage, wrapper);
        //每页的数据集合
        List<Columns> columnList = columnsPage.getRecords();
        //总的记录数
        Long total = columnsPage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", columnList);
        return R.ok("查询所有栏目分页数据成功").data("map", map);
    }

    @Operation(summary = "增加栏目")
    @PostMapping(value = "/addColumn")
    public R addColumn(Columns Column) {
        boolean flag = columnService.save(Column);
        if (flag) {
            return R.ok("栏目增加成功");
        } else {
            return R.ok("栏目增加失败");
        }
    }
    @Operation(summary = "删除栏目")
    @GetMapping(value = "/deleteColumn/{columnId}")
    public R deleteColumn(@PathVariable("columnId") Integer columnId) {
        boolean flag = columnService.removeById(columnId);
        if (flag) {
            return R.ok("栏目删除成功");
        } else {
            return R.ok("栏目删除失败");
        }
    }

    @Operation(summary = "按照栏目查看")
    @GetMapping(value = "/getColumnById/{columnId}")
    public R getColumnById(@PathVariable("columnId") Integer columnId) {
        LambdaJoinQueryWrapper<Columns> wrapper = new LambdaJoinQueryWrapper<>(Columns.class);
        wrapper.innerJoin(Columns.class, Columns::getParentColumnId, Columns::getColumnId);
        wrapper.eq(Columns::getColumnId, columnId);
        Columns Column = columnService.getOne(wrapper);
        if (Column != null) {
            return R.ok("按照栏目查看成功").data("Columns", Column);
        } else {
            return R.error("按照栏目查看失败");
        }
    }

    @Operation(summary = "修改栏目")
    @PostMapping(value = "/updateColumn")
    public R updateColumn(Columns Column) {
        boolean flag = columnService.updateById(Column);
        if (flag) {
            return R.ok("栏目修改成功");
        } else {
            return R.ok("栏目修改失败");
        }
    }

    @Operation(summary = "修改栏目的状态")
    @PostMapping(value = "/updateColumnStatus")
    public R updateRoleStatus(@RequestBody Columns column) {
        Integer status = column.getStatus();
        if (status == 1 || status == 0) {
            int newStatus = (status == 1) ? 0 : 1;
            column.setStatus(newStatus);
            boolean updated = columnService.updateById(column);

            String statusText = null;
            if (updated) {
                statusText = (newStatus == 1) ? "可用" : "不可用";
                return R.ok(statusText + column.getColumnName() + "成功").data("column", column);
            } else {
                return R.error(statusText + column.getColumnName() + "失败");
            }
        } else {
            return R.error("无效的状态值");
        }
    }
}